package com.udaos.modules.project.controllers;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.By;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.udaos.common.annotation.SLog;
import com.udaos.common.base.Result;
import com.udaos.common.filter.PrivateFilter;
import com.udaos.common.page.DataTableColumn;
import com.udaos.common.page.DataTableOrder;
import com.udaos.models.project.Ud_train_project;
import com.udaos.models.sys.Sys_user;
import com.udaos.modules.base.utils.EnumsUtils;
import com.udaos.modules.base.vo.SelectVo;
import com.udaos.modules.project.services.UdProjectService;


/**
 * @author mutaotao
 * @time 2017-01-18 11:17:20
 * 
 */
@IocBean
@At("/platform/project")
@Filters({ @By(type = PrivateFilter.class) })
public class UdProjectController {
	@Inject
	private UdProjectService udProjectService;

	/**
	 * 首页
	 */
	@At("")
	@Ok("beetl:/platform/base/project/index.html")
	@RequiresAuthentication
	public void index() {
		
	}
	/**
	 * 加载数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param order
	 * @param columns
	 * @return
	 */
	@At
	@Ok("json:full")
	@RequiresAuthentication
	public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
		Cnd cnd = Cnd.NEW();
		cnd.and("delFlag", "=", "0");
		return udProjectService.data(length, start, draw, order, columns, cnd, null);
	}
	
	/**
	 * 新增
	 */
	@At
	@Ok("beetl:/platform/base/project/add.html")
	@RequiresAuthentication
	public Object add() {
		Map<String,Object> map=new HashMap<String,Object>();
		List<SelectVo> drivings=EnumsUtils.getDrivingTypeEnum();
		List<SelectVo> subjects=EnumsUtils.getSubjectEnum();
		map.put("drivings", drivings);
		map.put("subjects", subjects);
		return map;
	}
	
	@At
	@Ok("json")
	@RequiresAuthentication
	@SLog(tag = "新增项目", msg = "项目名:${args[0].name}")
	public Object addDo(@Param("..") Ud_train_project ud_project, HttpServletRequest req) {
		try {
			ud_project.setCreateAt((int)(System.currentTimeMillis()/1000));
			Subject subject = SecurityUtils.getSubject();
            Sys_user user = (Sys_user) subject.getPrincipal();
            ud_project.setCreateBy(user.getId());
			udProjectService.insert(ud_project);
			return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@At("/edit/?")
	@Ok("beetl:/platform/base/project/edit.html")
	@RequiresAuthentication
	public Object edit(String id) {
		Map<String,Object> map=new HashMap<String,Object>();
		List<SelectVo> drivings=EnumsUtils.getDrivingTypeEnum();
		List<SelectVo> subjects=EnumsUtils.getSubjectEnum();
		map.put("drivings", drivings);
		map.put("subjects", subjects);
		map.put("project",udProjectService.fetch(id));
		return map;
	}
	
	@At
	@Ok("json")
	@RequiresAuthentication
	@SLog(tag = "修改项目", msg = "项目名:${args[0].name}")
	public Object editDo(@Param("..") Ud_train_project ud_project, HttpServletRequest req) {
		try {
			ud_project.setOpAt((int)(System.currentTimeMillis()/1000));
			udProjectService.updateIgnoreNull(ud_project);
			return Result.success("保存成功");
		} catch (Exception e) {
			return Result.error(e.getMessage());
		}
	}
	/**
	 * 删除
	 * @param id
	 * @param req
	 * @return
	 */
	@At("/delete/?")
    @Ok("json")
	@RequiresAuthentication
    @SLog(tag = "删除项目", msg = "用户名:${args[1].getAttribute('name')}")
    public Object delete(String id, HttpServletRequest req) {
        try {
        	Ud_train_project ud_project = udProjectService.fetch(id);
        	ud_project.setDelFlag(true);
        	udProjectService.updateIgnoreNull(ud_project);
            req.setAttribute("name", ud_project.getName());
            return Result.success("删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
	
	

}